// packageF/sachertorte/sachertorteBandShop/sachertorteBandShop.js
const app = getApp();
let location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    band: [],
    active: 0,

    screenShow:false,
    goodsList:[],
    cakeType:[
      {
        type:'',
        text:'全部'
      },
      {
        type:1,
        text:'4-5人食'
      },
      {
        type:2,
        text:'6-9人'
      },
      {
        type:3,
        text:'10-12人'
      },
      {
        type:4,
        text:'12人以上'
      }
    ],
    getBrand: [
    ],
    query: {
      page:1,
      city:'',
      order_field:'',
      order_by:'',
      cake_type:'',
      cake_price_max:'',
      cake_price_min: '',
      snacks_price_max:"",
      snacks_price_min: "",
    },
    totalPage:0,
    isLoadMore: true,
    brandsId:'',
    categorysId:'',
    keyword:'',
    priceSnacks:[
     {
      type: ["", ""],
      text:'全部',
     },
     {
      type: [20,99],
      text:'20-99',
     },
     {
      type: [100,199],
      text:'100-199',
     },
     {
      type: [199,''],
      text:'199以上',
     }
    ], 
    cakePriceArr:[
      {
        type: ['',''],
        text:'全部',
       },
       {
        type: [100,200],
        text:'100-200',
       },
       {
        type: [200,300],
        text:'200-300',
       }
    ],
    categorys: [],
    indexType:0, //尺寸选择
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      options:options
    })
    if(options.city) {
      this.setData({
        'query.city': options.city,
      })
    }
    if(options.brand_id) {
      this.setData({
        brandsId:options.brand_id
      })
    }
    this.getData();
    this.categorysList();
    this.goodsByList();
    this.getBrandsData();
  },
  tapStoreList(e){
    console.log(e)
    let id = e.currentTarget.dataset.key.id;
    wx.navigateTo({
      url: `/packageF/sachertorte/sachertorteSearch/sachertorteSearch?city=${this.data.query.city}&store_id=${id}`,
    })
  },
  getBandStore(){
    let json = { 
      brand_id: this.data.options.brand_id,
      lat:this.data.point.lat?this.data.point.lat:'',
      lng:this.data.point.lng?this.data.point.lng:''
    };
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.index.index.getStoreByBrandId");
    app._getNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          console.log(res)
          this.setData({
            BandStore:res.data.data,
            total_page:res.data.last_page,
            isLoadMore:true
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  screenClick(event) {  //筛选完成
    let {brandValue,cakeValue,categoryValue,priceValue,cakePirceValue} = event.detail;
    this.setData({
      screenShow: false,
      isLoadMore: true,
      brandsId:brandValue,
      categorysId:categoryValue,
      'query.cake_type': cakeValue,
      'query.page':1,
      'query.cake_price_min': cakePirceValue[0],
      'query.cake_price_max':cakePirceValue[1],
      'query.snacks_price_min': priceValue[0],
      'query.snacks_price_max':  priceValue[1],
    })
    this.goodsByList();
  },
  popularity() { //选择人气
    this.setData({
      'query.order_field': 'popularity',
      'query.order_by':'',
      'query.page':1,
      isLoadMore:true
    })
    this.goodsByList();
  },
  salesClick(event) {
    let sort = event.currentTarget.dataset.sort;
    let sortValue = 'asc';
    this.sortSwicth(sort,sortValue,'show_sales'); //处理判断倒序，正序
  },
  priceClick(event) { //选择价格
    let sort = event.currentTarget.dataset.sort;
    let sortValue = 'asc';
    this.sortSwicth(sort,sortValue,'price'); //处理判断倒序，正序
  },
  screeningPopup() {
    this.setData({ screenShow: true });
  },
  sortSwicth(sort,sortValue,field) {
    switch(sort) {
      case 'asc':
         sortValue = 'desc';
         break;
      case 'desc':
         sortValue = 'asc';
         break;
    }
    this.setData({
      'query.page':1,
      'query.order_by': sortValue,
      'query.order_field': field,
      isLoadMore:true
    })
    this.goodsByList();
  },
  categorysList() { //分类数据
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.index.index.getCategorys");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            categorys: res.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  goodsByList() {
    wx.showLoading({
      title: '加载中',
    })
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsList");
    urlStr += `&search[brand_id]=${this.data.brandsId}`;
    urlStr += `&search[category]=${this.data.categorysId}`;
    app._getNetWork({
      url: urlStr,
      data:this.data.query,
      success:  (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            goodsList: res.data.data,
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getBrandsData() {
    let query = {
      city: this.data.query.city
    }
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getBrand");
    app._getNetWork({
      url: urlStr,
      data:query,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            getBrand:res.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  tapChange(e){
    console.log(e)
    let key = e.currentTarget.dataset.key;
    this.setData({
      active:key,
      'query.page':1,
      isLoadMore:true,
      total_page:0
    })
    if (key == 1) {
      this.getBandStore();
    } else {
      this.goodsByList();
    }
  },
  getData(){
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getBrandDetails");
    app._getNetWork({
      url: urlStr,
      data: {
        brand_id:this.data.options.brand_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          this.setData({
            band:res.data
          })
          wx.setNavigationBarTitle({
            title: this.data.band.title,
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    try {
      this._getLocation((mapdata, e) => { //获取定位的城市，拿到定位的信息
        let city = mapdata.address_component.city;
        if(!this.data.query.city) {
          this.setData({
            'query.city':city
          });
        }
        //定位后调用首页接口和推荐列表接口
        this.categorysList();
        this.goodsByList();
        this.getBrandsData();
      });
    } catch (e) {
      console.log(e);
      this.categorysList();
      this.goodsByList();
      this.getBrandsData();
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})